home *** CD-ROM | disk | FTP | other *** search
/ El Mac 9 / El Mac 9.iso / Shareware / Demos / Igor Demo Pro / 1 PutContentsIn Igor Pro Folder / WaveMetrics Procedures / Waves / Load Row Data < prev    next >
Encoding:
Text File  |  1996-01-29  |  7.8 KB  |  222 lines  |  [TEXT/IGR0]

  1. //    This file contains procedures for loading data from text files when you want
  2. //    to store ROWs from the file in one or more 1D waves. The Igor file loaders normally
  3. //    store COLUMNs in 1D waves.
  4. //    Consider the following data:
  5. //        400    0    1    2    3    4
  6. //        405    5    6    7    8    9
  7. //        410    10    11    12    13    14
  8. //    The LoadRowDataInto1DWave routine loads this into a single 1D wave, optionally
  9. //    treating the first column as X values instead of as data.
  10. //    The LoadRowDataInto1DWaves routine loads this into a multiple 1D waves.
  11.  
  12. #pragma rtGlobals = 1
  13.  
  14. Menu "Macros"
  15.     "Load Row Data Into One 1D Wave", LoadRowDataInto1DWave()
  16.     "Load Row Data Into Multiple 1D Waves", LoadRowDataInto1DWaves()
  17. End
  18.  
  19.  
  20.  
  21. //    The LoadRowDataInto1DWave procedure loads the contents of a row-oriented
  22. //    text file into a single 1D wave. The text file is assumed to consist of values
  23. //    written as:
  24. //        <v00>    <v01>    <v02>    <v03> . . . <v0n>
  25. //        <v10>    <v11>    <v12>    <v13> . . . <v1n>
  26. //    and so on. The normal text file-loaders are column-oriented and would load
  27. //    this into a number of 1D waves. However, this data is intended to be loaded into
  28. //    a single 1D wave.
  29. //    Two cases are handled.
  30. //    In the first case, all of the data in each row is loaded into a 1D wave in row/column order.
  31. //    In the second case, the first column is taken to contain X values and is not loaded into the
  32. //    1D wave but instead is used to set the X scaling of the wave, on the assumption that the
  33. //    values in this column are uniformly-spaced. The remaining columns are loaded, again
  34. //    in row/column order.
  35. //
  36. //    To use the procedure, choose "Load Row Data Into One 1D Wave" from the Macros menu.
  37.  
  38. Function ExtractRowDataInto1DWave(mat, firstColNature, name)
  39.     Wave mat                        // Matrix containing row-oriented data
  40.     Variable firstColNature            // 1 = first column contains data, 2 = first column contains x values.
  41.     String name                        // Name of output wave
  42.     
  43.     Variable numRows, numColumns
  44.     Variable firstColIsX = firstColNature==2
  45.     
  46.     numRows = DimSize(mat, 0)
  47.     numColumns = DimSize(mat, 1)
  48.     
  49.     // Find points and columns to be included in output wave.
  50.     Variable firstCol = 0, lastCol = numColumns-1
  51.     Variable numPoints = numRows * numColumns
  52.     if (firstColIsX)
  53.         numPoints -= numRows        // Points from column 0 will not go into Y wave.
  54.         firstCol += 1
  55.     endif
  56.  
  57.     Make/O/D/N=(numPoints) $name
  58.  
  59.     // Store matrix data in the output wave.
  60.     Wave w = $name
  61.     Variable row = 0
  62.     Variable cols = lastCol-firstCol+1        // Number of columns to load.
  63.     Variable startPoint = 0                    // Start point in output wave for current row.
  64.     do
  65.         w[startPoint, startPoint+cols-1] = mat[row][firstCol+p-startPoint]
  66.         startPoint += cols
  67.         row += 1
  68.     while (row <= numRows-1)
  69.  
  70.     // If the first column is X, use it to set X scaling.
  71.     if (firstColIsX)
  72.         Variable x0, dx
  73.         x0 = mat[0][0]
  74.         dx = (mat[1][0]-x0) / cols
  75.         SetScale/P x x0, dx, "", w
  76.     endif
  77. End
  78.  
  79. Function FDeleteBlanksFromEnd(w)
  80.     Wave w
  81.     
  82.     Variable n = numpnts(w)
  83.     Variable i = n - 1
  84.     do
  85.         if (numtype(w[i]) != 2)            // Not a blank?
  86.             break
  87.         endif
  88.         i -= 1
  89.     while (i >= 0)
  90.     
  91.     Variable numBlanks = n - i - 1
  92.     if (numBlanks > 0)                        // Found some blanks?
  93.         DeletePoints i+1, numBlanks, w
  94.     endif
  95.     return numBlanks                        // Number of blanks removed
  96. End
  97.  
  98. Function FLoadRowDataInto1DWave(pathName, fileName, firstColNature, linesToSkip, linesToLoad, deleteBlanksAtEnd, name, makeTable)
  99.     String pathName                        // Name of path or "" for dialog.
  100.     String fileName                            // Name of file or "" for dialog.
  101.     Variable firstColNature                    // 1 = first column contains data, 2 = first column contains x values.
  102.     Variable linesToSkip                    // Number of lines to skip at start of file.
  103.     Variable linesToLoad                    // Number of lines to load or 0 for auto (load all lines).
  104.     Variable deleteBlanksAtEnd                // 1 = delete blanks, 2 = leave blanks at the end of the wave
  105.     String name                                // Name to use for new wave.
  106.     Variable makeTable                        // 1 == make a table showing new wave
  107.     
  108.     LoadWave/Q/J/M/D/N=tempLoadRowDataMatrix/P=$pathName/K=0/L={0,linesToSkip,0,0,0} fileName
  109.     ExtractRowDataInto1DWave(tempLoadRowDataMatrix0, firstColNature, name)
  110.     KillWaves tempLoadRowDataMatrix0
  111.     if (deleteBlanksAtEnd)                    // Useful when the last row of data is partial
  112.         FDeleteBlanksFromEnd($name)
  113.     endif
  114.     if (makeTable == 1)
  115.         Edit $name.id
  116.     endif
  117. End
  118.  
  119. Macro LoadRowDataInto1DWave(pathName, fileName, firstColNature, linesToSkip, deleteBlanksAtEnd, name, makeTable)
  120.     String pathName = "_none_"
  121.     Prompt pathName "Path", popup "_none_;" + PathList("*",";","")
  122.     String fileName = ""
  123.     Prompt fileName "File (\"\" for dialog)"
  124.     Variable firstColNature = 2
  125.     Prompt firstColNature "First column contains", popup "Data;X Values"
  126.     Variable linesToSkip = 0
  127.     Prompt linesToSkip "Lines to skip at start of file"
  128.     Variable deleteBlanksAtEnd = 1
  129.     Prompt deleteBlanksAtEnd, "Delete blanks at end of wave?", popup "Yes;No"
  130.     String name = "wave0"
  131.     Prompt name "Name for output wave"
  132.     Variable makeTable = 1
  133.     Prompt makeTable "Make table", popup "Yes;No"
  134.     
  135.     PauseUpdate; Silent 1
  136.     
  137.     if (CmpStr(pathName, "_none_") == 0)
  138.         pathName = ""                    // Causes LoadWave to display dialog.
  139.     endif
  140.     FLoadRowDataInto1DWave(pathName, fileName, firstColNature, linesToSkip, 0, deleteBlanksAtEnd, name, makeTable)
  141. End
  142.  
  143.  
  144. //    The LoadRowDataInto1DWaves procedure loads the contents of a row-oriented
  145. //    text file into multiple 1D waves. The text file is assumed to consist of values
  146. //    written as:
  147. //        <v00>    <v01>    <v02>    <v03> . . . <v0n>
  148. //        <v10>    <v11>    <v12>    <v13> . . . <v1n>
  149. //    LoadRowDataInto1DWaves would load this into two 1D waves.
  150. //    To use the procedure, choose "Load Row Data Into Multiple 1D Waves" from the Macros menu.
  151.  
  152. Function ExtractRowDataInto1DWaves(mat, baseName)
  153.     Wave mat                        // Matrix containing row-oriented data
  154.     String baseName                // Base name of output waves
  155.     
  156.     Variable numRows, numColumns
  157.     Variable row
  158.     String name
  159.     
  160.     numRows = DimSize(mat, 0)
  161.     numColumns = DimSize(mat, 1)
  162.     
  163.     row = 0
  164.     do
  165.         name = baseName + num2istr(row)
  166.         Make/O/D/N=(numColumns) $name
  167.     
  168.         // Store matrix data in the output wave.
  169.         Wave w = $name
  170.         w = mat[row][p]
  171.         row += 1
  172.     while (row <= numRows-1)
  173. End
  174.  
  175. Function FLoadRowDataInto1DWaves(pathName, fileName, linesToSkip, linesToLoad, columnsToSkip, columnsToLoad, baseName, makeTable)
  176.     String pathName                        // Name of path or "" for dialog.
  177.     String fileName                            // Name of file or "" for dialog.
  178.     Variable linesToSkip                    // Number of lines to skip at start of file.
  179.     Variable linesToLoad                    // Number of lines to load or 0 for auto (load all lines).
  180.     Variable columnsToSkip                    // Number of columns to skip.
  181.     Variable columnsToLoad                    // Number of columns to load or 0 for auto (load all columns).
  182.     String baseName                        // Base name to use for new waves.
  183.     Variable makeTable                        // 1 == make a table showing new waves.
  184.     
  185.     LoadWave/Q/J/M/D/A=tempLoadRowDataMatrix/P=$pathName/K=0/L={0,linesToSkip,linesToLoad,columnsToSkip,columnsToLoad} fileName
  186.     ExtractRowDataInto1DWaves(tempLoadRowDataMatrix0, baseName)
  187.     Variable numRows = DimSize(tempLoadRowDataMatrix0, 0)
  188.     KillWaves tempLoadRowDataMatrix0
  189.     if ((makeTable==1) %& (numRows>0))
  190.         Variable row = 0
  191.         String name
  192.         Edit
  193.         do
  194.             name = baseName + num2istr(row)
  195.             AppendToTable $name
  196.             row += 1
  197.         while (row <= numRows-1)
  198.     endif
  199. End
  200.  
  201. Macro LoadRowDataInto1DWaves(pathName, fileName, linesToSkip, columnsToSkip, baseName, makeTable)
  202.     String pathName = "_none_"
  203.     Prompt pathName "Path", popup "_none_;" + PathList("*",";","")
  204.     String fileName = ""
  205.     Prompt fileName "File (\"\" for dialog)"
  206.     Variable linesToSkip = 0
  207.     Prompt linesToSkip "Lines to skip at start of file"
  208.     Variable columnsToSkip = 0
  209.     Prompt columnsToSkip "Columns to skip"
  210.     String baseName = "wave"
  211.     Prompt baseName "Base name for output waves"
  212.     Variable makeTable = 1
  213.     Prompt makeTable "Make table", popup "Yes;No"
  214.     
  215.     PauseUpdate; Silent 1
  216.     
  217.     if (CmpStr(pathName, "_none_") == 0)
  218.         pathName = ""                    // Causes LoadWave to display dialog.
  219.     endif
  220.     FLoadRowDataInto1DWaves(pathName, fileName, linesToSkip, 0, columnsToSkip, 0, baseName, makeTable)
  221. End
  222.